import { DRACOLoader, GLTFLoader } from "three/examples/jsm/Addons.js";
import type Viewer from "../viewer";


export default class ModelManager {
  public viewer: Viewer
  public loaderGltf: GLTFLoader
  public dracoLoader: DRACOLoader
  constructor(viewer: Viewer) {
    this.viewer = viewer;
    this.loaderGltf = new GLTFLoader();
    this.dracoLoader = new DRACOLoader()
    // 提供一个DracLoader实例来解码压缩网格数据
    this.dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
    this.loaderGltf.setDRACOLoader(this.dracoLoader)
  }

  /**
  * 添加模型数据
  * @param url 模型的路径
  * @param callback 返回模型对象，常用一些功能挂接在模型对象上
  * @param progress 返回加载进度，还有问题，需要修改
  */
  loadModelToScene(url: string, callback?: (gltf: any) => void, progress?: (str: string) => void) {
    this.loaderGltf.load(url, gltf => {
      this.viewer.graphWorld.add(gltf.scene.children[0]);
      callback && callback(gltf)
    }, xhr => {
      progress && progress((xhr.loaded / xhr.total).toFixed(2)) // 输出加载进度
    })
  }
}